define([
    'zepto',
    'underscore',
    'backbone',
    "text!templates/newCard.html",
    "text!templates/cardListArrow.html",
    "text!templates/bankList.html",
    "text!templates/home.html",
    "wTools"

], function ($, _, Backbone, template, cardList, bankList, order) {
    return Backbone.View.extend({
        $el: $main,
        el: $main0,
        template: _.template(template),
        templateCardList: _.template(cardList),
        templateBankList: _.template(bankList),
        templateOrder: _.template(order),
        events: {
            "tap #nextStep": "nextStep",
            "tap #card": "Card",
            "tap .chooseCardBg": "closeList",
            "tap #cardListOut": "closeList",
            "tap .cardList li.chooseCard1": "chooseCard",
            "tap #bankClick": "bankList",
            "tap #closeBankList": "closeBankList"
        },
        initialize: function () {
            if (wData.urlData.token != undefined && wData.urlData.token != "" && wData.urlData.token != null && wData.urlData.token != wData.token) {
                wData.token = wData.urlData.token;
            }
            if (wData.token != undefined && wData.token != "" && wData.token != null) {
                $.cookie("token", wData.token, {path: "/"});
            } else if ($.cookie("token") != undefined && $.cookie("token") != "" && $.cookie("token") != null) {
                wData.token = $.cookie("token");
            } else {
                wData.errorMessage = "操作超时,请重试";
                location.hash = "fail";
            }


            if (!($("#order").length > 0)) {
                if (wData.orderInfo == undefined || wData.orderInfo == '' || wData.orderInfo == null) {
                    var postData = {
                        token: wData.token
                    }, that = this;
                    wAjaxForm("getOrderInfo", "", postData, function (result) {
                        if (result.success == true) {
                            wData.noCard = false;
                            wData.cardList = result.data.qpayCardInfos;
                            wData.orderInfo = result.data.tradeVoucherList[0];
                            wData.orderInfoTouchuan={
                                payerName:"",
                                idNumber:"",
                                phoneNumber:"",
                                payerCardNo:"",
                                cardType:""
                            };
                            if(result.data.buyerInfo!=undefined&&result.data.buyerInfo!=null) {
                                wData.orderInfoTouchuan = result.data.buyerInfo;
                            }
                            $("#home").append(that.templateOrder(wData.orderInfo));
                            that.render();
                            if (!wData.cardList.length > 0) {
                                wData.noCard = true;
                                $("#card").hide();
                                $("#head_back").hide();
                            }
                            if(wData.orderInfoTouchuan.cardType!=""&&wData.orderInfoTouchuan.cardType!=undefined&&wData.orderInfoTouchuan.cardType!=null) {
                                if (wData.orderInfoTouchuan.cardType == "DC") {
                                    $("#cardNo").attr("placeholder", "储蓄卡");
                                } else if (wData.orderInfoTouchuan.cardType == "CC") {
                                    $("#cardNo").attr("placeholder", "信用卡");
                                }
                            }
                        }
                        else {
                            wTips(result.message);
                        }
                    });
                } else {
                    $("#home").append(this.templateOrder(wData.orderInfo));
                    if(wData.orderInfoTouchuan.cardType!=""&&wData.orderInfoTouchuan.cardType!=undefined&&wData.orderInfoTouchuan.cardType!=null) {
                        if (wData.orderInfoTouchuan.cardType == "DC") {
                            $("#cardNo").attr("placeholder", "储蓄卡");
                        } else if (wData.orderInfoTouchuan.cardType == "CC") {
                            $("#cardNo").attr("placeholder", "信用卡");
                        }
                    }
                }

            } else {

                this.render();
                if (wData.noCard) {
                    $("#card").hide();
                    $("#head_back").hide();
                }
                if(wData.orderInfoTouchuan.cardType!=""&&wData.orderInfoTouchuan.cardType!=undefined&&wData.orderInfoTouchuan.cardType!=null) {
                    if (wData.orderInfoTouchuan.cardType == "DC") {
                        $("#cardNo").attr("placeholder", "储蓄卡");
                    } else if (wData.orderInfoTouchuan.cardType == "CC") {
                        $("#cardNo").attr("placeholder", "信用卡");
                    }
                }
            }
        },
        pageOn: "base",
        //直接付款下一步
        nextStep: function () {
            if (wCheckBool($("#cardNo").val(), "^\\d{8,32}$", "", "银行卡号")) {
                var cardtype2="";
                if(wData.orderInfoTouchuan.cardType!=""&&wData.orderInfoTouchuan.cardType!=undefined&&wData.orderInfoTouchuan.cardType!=null){
                    cardtype2=wData.orderInfoTouchuan.cardType;
                }
                var postData = {
                    cardNo: $("#cardNo").val(),
                    partnerId: wData.orderInfo.partnerId,
                    productCode: wData.orderInfo.productCode,
                    accessChannel: "WAP",
                    dbcr: cardtype2
                }, that = this;
                wAjaxForm("checkBankCard", "", postData, function (result) {
                    if (result.success == true) {
                        if (result.data.returnCode == "101") {
                            wData.cardNo = $("#cardNo").val();
                            wData.otherCard = result.data.payChannelList[0];
                            wData.selectCardIndex = "-1";
                            wData.cardType = wData.otherCard.dbcr;
                            //that.$el.append(that.templateMyCard(wData));
                            that.more();
                            location.hash = "home";
                        } else {
                            wTips("银行卡号格式不正确");
                        }
                    }
                    else {
                        wTips(result.message);
                    }
                });
            }

        },
        //查看订单信息是否展开,若展开则关闭
        more: function () {
            if ($("#more").html() == "收起") {
                $("#order").css({"height": "2.6rem"});
                $("#more").html("更多");
            }
        },
        //打开其他银行卡列表
        Card: function () {
            this.$el.append(this.templateCardList());
            $(".chooseCardBg").css({"height": $(window).height()});
            $("#iconList_" + wData.selectCardIndex).removeClass("check").addClass("noCheck");
        },
        //更改为其他银行卡
        chooseCard: function (e) {

            $("#cardList").remove();
            $("#order_content").remove();
            wData.selectCardIndex = e.currentTarget.id;
            wData.selectCard = wData.cardList[wData.selectCardIndex];
            //this.$el.append(this.templateMyCard(wData.selectCard));
            this.more();
            location.hash = "home";
        },
        //未做更改,关闭银行卡列表
        closeList: function () {
            $("#cardList").remove();
        },

        //打开查看支持银行列表
        bankList: function () {
            var postData = {
                cardNo: "1111111111111111",
                partnerId: wData.orderInfo.partnerId,
                productCode: wData.orderInfo.productCode,
                accessChannel: "WAP"
            }, that = this;
            wAjaxForm("checkBankCard", "", postData, function (result) {
                if (result.success == true) {
                    if (result.data.returnCode == "102") {
                        wData.bankList = result.data.payChannelList;
                        that.$el.append(that.templateBankList(wData.bankList));
                        $(".chooseBankBg").css({"height": ($(window).height() - $("#head").height())});
                        $(".bankList").css({"height": ($(window).height() - $("#head").height() * 3.6)});
                        App.title("支持银行");
                        that.pageOn = "bankList";
                    }
                }
                else {
                    wTips("获取支持银行列表失败");
                }
            });
        },
        //关闭支持银行列表
        closeBankList: function () {
            $("#bankList").remove();
            App.title("订单支付");
            this.pageOn = "base";
        },
        back: function () {
            if (this.pageOn == "base") {
                this.more();
                history.back();
            } else if (this.pageOn == "bankList") {
                this.closeBankList();
            }
        },
        render: function (data) {
            var html = this.template(data);
            this.$el.html(html);
        },
        destroy: function () {
            this.undelegateEvents();
            this.$el.html("");
            $("#head_back").show();
            delete this;
        }
    });
});